import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_public/flutter_public.dart';
import 'package:provider/provider.dart';

import 'model/test_model.dart';
import 'notifier/test_list_notifier.dart';
import 'test_detail_page.dart';
import 'widget/test_filter_drawer.dart';

/// FileName test_list_page
///
/// @Author lax
/// @Date 2024/6/4
///
/// @Description

class TestListPage extends StatefulWidget {
  final bool showAppBar;

  const TestListPage({super.key, this.showAppBar = true});

  @override
  State<StatefulWidget> createState() {
    return TestListPageState();
  }
}

class TestListPageState extends State<TestListPage> {
  late TestListNotifier _notifier;

  @override
  void initState() {
    super.initState();
    _notifier = TestListNotifier();
  }

  void refreshData() {
    _notifier.refreshData();
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<TestListNotifier>(
      create: (_) => _notifier,
      child: Consumer<TestListNotifier>(builder: (context, notifier, child) {
        return Scaffold(
          appBar: widget.showAppBar
              ? PublicAppBar(
                  title: '列表',
                  actions: [filterButton()],
                )
              : null,
          body: PageIndicator(
            pageStatus: notifier.pageStatus,
            isEmpty: notifier.dataSource.isEmpty,
            loadingShowChild: true,
            errorShowChild: true,
            child: body(),
          ),
          endDrawer: TestFilterDrawer(
            model: notifier.filterModel,
            onFilter: (model) {
              Navigator.pop(context);
              notifier.filterModel = model;
              notifier.refreshData();
            },
          ),
          // floatingActionButton: FloatingActionButton(
          //   child: const Icon(Icons.add),
          //   onPressed: () {
          //     Navigator.push(context, MaterialPageRoute(builder: (context) {
          //       return const TestDetailPage();
          //     })).then((value) {
          //       if (value != null) {
          //         _notifier.refreshData();
          //       }
          //     });
          //   },
          // ),
        );
      }),
    );
  }

  Widget body() {
    return EasyRefresh(
      controller: _notifier.easyRefreshController,
      onRefresh: () async {
        _notifier.refreshData();
      },
      onLoad: () async {
        _notifier.loadData();
      },
      child: listView(),
    );
  }

  Widget filterButton() {
    return Builder(builder: (BuildContext context) {
      return TextButton(
        child: const Icon(
          Icons.filter_alt_outlined,
          color: Colors.white,
        ),
        onPressed: () {
          Scaffold.of(context).openEndDrawer();
        },
      );
    });
  }

  Widget listView() {
    return ListView.separated(
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 16),
      itemBuilder: (context, index) {
        final model = _notifier.dataSource[index];
        return GestureDetector(
          onTap: () {
            Navigator.push(context, MaterialPageRoute(builder: (context) {
              return TestDetailPage(model: model);
            })).then((value) {
              if (value != null) {
                _notifier.refreshData();
              }
            });
          },
          onLongPress: () {
            if (model.enable) {
              PublicAlertDialog.show(
                context: context,
                title: '确认删除吗？',
                onConfirm: () async {
                  _notifier.deleteAction(model);
                },
              );
            }
          },
          child: listItem(model),
        );
      },
      separatorBuilder: (context, index) {
        return const SizedBox(height: 16);
      },
      itemCount: _notifier.dataSource.length,
    );
  }

  Widget listItem(TestModel model) {
    return RadiusShadowContainer(
      padding: const EdgeInsets.all(16),
      child: Column(
        children: [
          Text(model.question ?? ''),
          Text('status:${model.statusDesc ?? ''}'),
          Text(model.createDate ?? ''),
        ],
      ),
    );
  }
}
